package com.dify.knowledge.repository;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.dify.knowledge.entity.ManualAnswer;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 人工解答Mapper接口
 */
@Mapper
public interface ManualAnswerMapper extends BaseMapper<ManualAnswer> {
    
    /**
     * 根据未解答问题ID查询所有人工解答
     * @param questionId 未解答问题ID
     * @return 人工解答列表
     */
    @Select("SELECT * FROM manual_answer WHERE unanswered_question_id = #{questionId} ORDER BY created_at DESC")
    List<ManualAnswer> selectByQuestionId(@Param("questionId") Long questionId);
    
    /**
     * 获取指定问题的最高分答案（有用数-无用数最高）
     * @param questionId 未解答问题ID
     * @return 最高分答案
     */
    @Select("SELECT * FROM manual_answer WHERE unanswered_question_id = #{questionId} " +
            "ORDER BY (useful_count - useless_count) DESC, useful_count DESC LIMIT 1")
    ManualAnswer selectTopRatedByQuestionId(@Param("questionId") Long questionId);
}
